$(document).ready(function(){
    setTimeout("closeResultMessages()",5000);
    if(typeof(bkLib)!='undefined'){
        bkLib.onDomLoaded(function(){
            if(typeof(document.getElementById("description"))!='undefined'){
                var descriptionEditor = new nicEditor({
                    fullPanel : true,
                    maxHeight:500
                }).panelInstance('description');
                descriptionEditor.addEvent("blur", function() {
                    document.getElementById("description").value = descriptionEditor.instanceById("description").getContent();
                });
            }
        });
    }
    if($('#thumb').length>0){
        $('#thumb').uploadify({
            'uploader'  : '/admin/themes/default/js/uploadify/uploadify.swf',
            'script'    : '/admin/themes/default/js/uploadify/lib_upload.php',
            'cancelImg' : '/admin/themes/default/js/uploadify/cancel.png',
            'buttonImg' : '/admin/themes/default/js/uploadify/btnupload.gif',
            'fileExt'   : '*.jpg;*.gif;*.png',
            'auto'      : false,
            'removeCompleted' : true,
            'width'      : 95,
            'height'     : 23,
            'scriptData' : {
                'upload_type':'thumb'
            },
            'onSelect' : function(event,ID,fileObj,data) {
                if(fileObj.name!='' && $('#isUploadImages').val()!=1){
                    $('#isUploadthumb').val("1");
                    $('#addPhotosSubmit').attr('disabled', false);
                }
            },
            'onCancel': function(event,ID,fileObj,data) {
                if(data.fileCount==0){
                    $('#isUploadthumb').val("0");
                }
            },
            'onAllComplete' : function(event,data) {
                if($('#isUploadImages').val()=="0"){
                    location.reload(true);
                }
            }
        });
    }
    if($('#images').length>0){
        $('#images').uploadify({
            'uploader'  : '/admin/themes/default/js/uploadify/uploadify.swf',
            'script'    : '/admin/themes/default/js/uploadify/lib_upload.php',
            'cancelImg' : '/admin/themes/default/js/uploadify/cancel.png',
            'buttonImg' : '/admin/themes/default/js/uploadify/btnupload.gif',
            'fileExt'   : '*.jpg;*.gif;*.png',
            'multi'     : true,
            'queueSizeLimit' : 100,
            'auto'      : false,
            'removeCompleted' : true,
            'width'      : 95,
            'height'     : 23,
            'queueID'    : 'imagesList',
            'scriptData'  : {
                'upload_type':'images'
            },
            'onSelect' : function(event,ID,fileObj,data) {
                if(fileObj.name!='' && $('#isUploadImages').val()!=1){
                    $('#isUploadImages').val("1");
                }
            },
            'onCancel': function(event,ID,fileObj,data) {
                if(data.fileCount==0){
                    $('#isUploadImages').val("0");
                }
            },
            'onAllComplete' : function(event,data) {
                location.reload(true);
            }
        });
    }
    
});


function submitFormAddEditLibrary(type){
    
    if($("#title").val()==''){
        $("#titleAlert").show();
        setTimeout("closeAlertMessages('titleAlert')",8000);
        $("#title").focus();
        return false;
    }

    if($("#categoryId").val()==''){
        $("#categoryAlert").show();
        setTimeout("closeAlertMessages('categoryAlert')",8000);
        $("#categoryId").focus();
        return false;
    }
    if(type=="add"){
        if($("#isUploadthumb").val()=="0"){
            $("#thumbAlert").show();
            setTimeout("closeAlertMessages('thumbAlert')",8000);
            return false;
        }
        if($("#isUploadImages").val()=="0"){
            $("#imagesAlert").show();
            setTimeout("closeAlertMessages('imagesAlert')",8000);
            return false;
        }
    }
    
//    if($("#description").val()=='' || $("#description").val()=='<br>'){
//        $("#descriptionAlert").show();
//        setTimeout("closeAlertMessages('descriptionAlert')",80000);
//        $("#description").focus();
//        return false;
//    }

    var data = "";
    if(type=="edit"){
        data = 'action=editAjax';
        data = data + '&removeImage=' + $("#imageRemove").val();
    }else{
        data = 'action=addAjax';
    }
    
    data = data + '&library_id=' + $("#library_id").val();
    data = data + '&title=' + $("#title").val();
    data = data + '&categoryId=' + $("#categoryId").val();
    data = data + '&area=' + $("#area").val();
    data = data + '&floor=' + $("#floor").val();
    data = data + '&bedroom=' + $("#bedroom").val();
    data = data + '&active=' + $("#active").val();
    data = data + '&description=' + $("#description").val();
    $.ajax({
        type: "POST",
        url: "/cp-admin/library",
        data: data,
        success: function(lib_id){
            if(type=="add"){
                $('#thumb').uploadifySettings('scriptData',{
                    'library_id':lib_id
                });
                $('#thumb').uploadifyUpload();
                $('#images').uploadifySettings('scriptData',{
                    'library_id':lib_id
                });
                $('#images').uploadifyUpload();
            }
            if(type=="edit"){
                if($('#isUploadthumb').val()=="1"){
                    $('#thumb').uploadifySettings('scriptData',{
                        'library_id':lib_id,
                        'edit':1
                    });
                    $('#thumb').uploadifyUpload();
                }
                if($('#isUploadImages').val()=="1"){
                    $('#images').uploadifySettings('scriptData',{
                        'library_id':lib_id,
                        'edit':1,
                        'removeImage':$("#imageRemove").val()
                    });
                    $('#images').uploadifyUpload();
                }
                if($('#isUploadthumb').val()=="0" && $('#isUploadImages').val()=="0"){
                    location.reload(true);
                }
            }
            
        }
    });
    return false;

}


function deleteItem(id,txtAlert){
    if (confirm("Do you want to delete "+txtAlert+"?")){
        $.ajax({
            url: '/'+ADMIN_SECTION+'/library',
            type: 'POST',
            data: "library_id="+id+"&action=deleteAjax",
            error: function(){
                alert('Error loading article infomation');
            },
            success: function(){
                window.location.reload(true);
            }
        });
    }
}

function removeImage(id){
    if($('#imageRemove').val()==""){
        $('#imageRemove').val(id);
    }else{
        var newVal = $('#imageRemove').val()+","+id;
        $('#imageRemove').val(newVal);
    }
    $("#img"+id).remove();
    $("#aimg"+id).remove();
}

function closeResultMessages(){
    if ( $("#rsMessages").length > 0 ) {
        document.getElementById('rsMessages').style.display = 'none';
    }
}

function closeAlertMessages(id){
    document.getElementById(id).style.display = 'none';
}

checked=false;
function checkedAll (formId) {
    var aa= document.getElementById(formId);
    if (checked == false)        {
        checked = true;
    }else{
        checked = false;
    }
    for (var i =0; i < aa.elements.length; i++){
        aa.elements[i].checked = checked;
    }
}

function actionMultil(action){
    if(action=='delete'){
        if (confirm("Do you want to delete these articles?")){
            document.getElementById('formView').submit();
        }else{
            document.getElementById('action').value = '';
        }
    }else{
        document.getElementById('formView').submit();
    }
    
}
